package com.droidbank;

import java.util.Date;

import com.droidbank.model.Account;
import com.droidbank.model.DataModel;
import com.droidbank.model.Expense;
import com.droidbank.util.FormatDecimal;
import com.droidbank.util.UIUtil;

import android.content.DialogInterface;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class AddExpenseActivity extends Activity {

	public void onCreate(Bundle saved) {
        super.onCreate(saved);
        setContentView(R.layout.add_layout);
        
        final Spinner spin = (Spinner) findViewById(R.id.accountSpinner);
        final ArrayAdapter<String> spinAdap = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
        spinAdap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(spinAdap);
        
        final DataModel dm = (DataModel)this.getApplication();
        
        for(Account a: dm.getAccounts())
        	spinAdap.add(a.getName());
        
        final AutoCompleteTextView name = (AutoCompleteTextView) findViewById(R.id.nameAutoEdit);
        //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.type_list_item, DataModel.getExpenseTypes());
       // name.setAdapter(adapter);
        
        
        final EditText type = (EditText) findViewById(R.id.typeEdit);
        type.setOnLongClickListener(new OnLongClickListener() {

            public boolean onLongClick(View v) {

              final CharSequence cs[] =UIUtil.convertVectorCS(DataModel.getExpenseTypes());
              AlertDialog.Builder builder = new AlertDialog.Builder(AddExpenseActivity.this);
              builder.setTitle("Types");
              builder.setItems(cs, new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int item) {
                      type.setText(cs[item]);
                  }
              });
              builder.create().show();
              return true;
            }
        });
        
        
        name.setOnLongClickListener(new OnLongClickListener() {

            public boolean onLongClick(View v) {

            if(!type.getText().toString().isEmpty())	
            {
            	
              final CharSequence cs[] =UIUtil.convertVectorCS(dm.getListOfNamesFromType(type.getText().toString()));
              AlertDialog.Builder builder = new AlertDialog.Builder(AddExpenseActivity.this);
              builder.setTitle("Names");
              builder.setItems(cs, new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int item) {
                      name.setText(cs[item]);
                  }
              });
              builder.create().show();
            }
            
            return true;
            }
        });
        
        final EditText amount = (EditText) findViewById(R.id.amountEdit);
        final CheckBox deposit = (CheckBox)  findViewById(R.id.depositCheck);
        
        final Button add = (Button) findViewById(R.id.addButton);
        add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) 
            {
            	

            	
            	String nameText = name.getText().toString();
            	String typeText = type.getText().toString();
            	String amountText = amount.getText().toString();
            	
            	if(!nameText.isEmpty() &&
            	   !typeText.isEmpty() &&
            	   !amountText.isEmpty() && 
            	   !spinAdap.isEmpty())
            	{
            		double amountd = Double.valueOf(amountText);
            		if(!deposit.isChecked() && amountd > 0)
            		{
            			amountd*=-1;
            		}
            		
            		Account a = dm.getAccounts().get(spin.getSelectedItemPosition());
            		a.addExpense(new Expense(new Date().getTime(), typeText, nameText, "",
            				FormatDecimal.format( 2, amountd)));
            		
            		DataModel.isDirty = true;
            		
            		Toast t = Toast.makeText(AddExpenseActivity.this,"Added Expense", 
                			Toast.LENGTH_SHORT);
            		t.setGravity(Gravity.CENTER, 0, 0);
            		t.show();
            	}
            	else
            	{
            		Toast t = Toast.makeText(AddExpenseActivity.this,"Missing Field", 
                			Toast.LENGTH_SHORT);
            		t.setGravity(Gravity.CENTER, 0, 0);
            		t.show();
            	}
            	
            	
            }
        });
        
	}
}
